import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:word_app/app/modules/model/app_https_client.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'dart:io';

import 'package:word_app/app/modules/model/toast_model.dart';

class FeedbackController extends GetxController {
  RxList<Widget> imgaesArr = <Widget>[].obs;
  TextEditingController tfVc = TextEditingController();
  String tfStr = '';
  List<XFile> imgFiles = [];
  AppHttpsClient httpsClient = AppHttpsClient();
  //选择相片
  photoGallery() async {
    final pickedFile =
        await ImagePicker().pickImage(source: ImageSource.gallery);
    if (pickedFile == null) {
      return;
    }
    compressXFile(pickedFile).then((value) {
      imgFiles.add(value!);
    });

    imgaesArr.insert(
        0,
        ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: Image.file(
            File(pickedFile.path),
            fit: BoxFit.cover,
          ),
        ));
    update();
  }

  //拍照
  photoCamera() async {
    final pickedFile =
        await ImagePicker().pickImage(source: ImageSource.camera);

    if (pickedFile == null) {
      return;
    }
    compressXFile(pickedFile).then((value) {
      imgFiles.add(value!);
    });
    imgaesArr.insert(
        0,
        ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: Image.file(File(pickedFile.path), fit: BoxFit.cover),
        ));
    update();
  }

  //添加图片按钮
  addPhotoWidget() {
    return Container(
      decoration: BoxDecoration(
          color: const Color.fromARGB(255, 239, 239, 239),
          borderRadius: BorderRadius.circular(10)),
      child: Container(
        margin: const EdgeInsets.all(40),
        child: Image.asset(
          "images/addImage.png",
          fit: BoxFit.fill,
        ),
      ),
    );
  }

  submitData() async {
    var response = await httpsClient.uploadImage(imgFiles,
        "user/feedback/create", {"feedback": tfVc.text, "type": "suggestion"});
    if (response != null) {
      if (response.statusCode == 200) {
        Get.back();
        ToastModel.showToast("提交成功");
      } else {
        ToastModel.showToast("提交失败");
      }
    } else {
      ToastModel.showToast("提交失败");
    }
  }

  Future<XFile?> compressXFile(XFile xFile) async {
    // 获取图片路径
    final String imagePath = xFile.path;

    // 使用 flutter_image_compress 压缩图片
    final result = await FlutterImageCompress.compressWithFile(
      imagePath,
      minWidth: 800, // 设置最小宽度
      minHeight: 600, // 设置最小高度
      quality: 88, // 设置压缩质量
    );

    if (result != null) {
      // 将压缩后的数据写入临时文件
      final tempDir = Directory.systemTemp;
      final tempFile = File('${tempDir.path}/compressed_image.jpg')
        ..writeAsBytesSync(result);

      // 返回压缩后的图片路径作为 XFile
      return XFile(tempFile.path);
    } else {
      print("图片压缩失败");
      return null;
    }
  }

  @override
  void onInit() {
    super.onInit();
    imgaesArr.add(addPhotoWidget());
    update();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
